package top.blueShark.template.satoken;

import java.io.Serializable;

/**
 * ajax请求返回Json格式数据的封装 
 */
public class AjaxJson implements Serializable {

	private static final long serialVersionUID = 1L;	// 序列化版本号
	
	public int code;      // 状态码
	public Object data;   // 携带对象
	public Object error;  // 错误信息
	public String message; // 描述信息

	// getter/setter
	public int getCode() { return code; }
	public AjaxJson setCode(int code) { this.code = code; return this; }

	public Object getData() { return data; }
	public AjaxJson setData(Object data) { this.data = data; return this; }

	public AjaxJson setError(Object error) { this.error = error; return this; }

	public String getMessage() { return message; }
	public AjaxJson setMessage(String message) { this.message = message; return this; }

	// 构造方法
	public AjaxJson(int code, String message, Object data, Object error) {
		this.code = code;
		this.message = message;
		this.data = data;
		this.error = error;
	}

	// 静态工厂方法
	public static AjaxJson getSuccess() {
		return new AjaxJson(0, "ok", null, null);
	}
	public static AjaxJson getSuccess(String message, Object data) {
		return new AjaxJson(0, message, data, null);
	}
	public static AjaxJson getSuccessData(Object data) {
		return new AjaxJson(0, "ok", data, null);
	}
	public static AjaxJson getError(String message, Object error) {
		return new AjaxJson(1, message, null, error);
	}
	public static AjaxJson getError(String message) {
		return new AjaxJson(1, message, null, null);
	}
	public static AjaxJson getError() {
		return new AjaxJson(1, "error", null, null);
	}

	@Override
	public String toString() {
		return "{" +
				"\"code\": " + code +
				", \"data\": " + (data == null ? "null" : data.toString()) +
				", \"error\": " + (error == null ? "null" : error.toString()) +
				", \"message\": \"" + message + "\"" +
				'}';
	}
}
